<template>
  <div class="page">
    <!-- <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form> -->
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addVideo">新增视频</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border="" style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <!-- <el-table-column prop="videourl" label="视频URL"></el-table-column> -->
      <el-table-column prop="videoindex" label="视频序号"></el-table-column>
      <el-table-column prop="name" label="所有人"></el-table-column>
      <el-table-column prop="imgurl" label="视频封面">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.imgurl"
            :preview-src-list="[$Url + '/upload/' + scop.row.imgurl]"></el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="stuname" label="学员名字"></el-table-column> -->
      <el-table-column prop="title" label="视频标题"></el-table-column>
      <el-table-column prop="isEffect" label="是否生效" width="100">
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.isEffect == 1">生效</el-tag>
          <el-tag type="warning" v-if="scop.row.isEffect == 2">失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="上传时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间" width="200"></el-table-column>
      <el-table-column prop label="操作" width="220">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="info(scop.row)"></el-button>
          <el-button type="warning" icon="el-icon-view" circle @click="look(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.videoid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
    <el-dialog title="H5首页视频" center :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="addeditform" :rules="rules" ref="addForm" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="学员" prop="stuid" v-if="!isEdit">
          <!-- <el-input v-model="addeditform.stuid" placeholder="请输入内容"></el-input> -->
          <check-stu :itemId="addeditform.stuid" @checkedItem="checkedItem"/>
        </el-form-item>
        <el-form-item label="视频封面" prop="imgurl" v-if="!isEdit">
          <upload v-model="addeditform.imgurl" />
        </el-form-item>
        <el-form-item label="视频内容" v-if="!isEdit" prop="videourl">
          <upload v-model="addeditform.videourl" type="video"/>
        </el-form-item>
        <el-form-item label="视频标题" prop="title">
          <el-input v-model="addeditform.title" placeholder="请输入视频标题" clear=""></el-input>
        </el-form-item>
        <el-form-item label="视频序号" prop="videoindex">
          <el-input v-model="addeditform.videoindex" placeholder="请设置视频序号" clear=""></el-input>
        </el-form-item>
        <el-form-item label="是否生效" prop="isEffect">
          <el-switch v-model="addeditform.isEffect" :active-value="1" :inactive-value="2" active-text="是" inactive-text="否"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="视频预览" center :visible.sync="dialogLook" width="50%" @close="closeLook">
      <video :src="$Url + '/upload/' + addeditform.videourl" width="100%" height="500px" controls ref='videoPlayer'></video>
    </el-dialog>
  </div>
</template>
<script>
import { getStuVideo, updateStuVideo, addStuVideo, delStuVideo} from "@/api/user";
import upload from "@/components/uploadFile";
import checkStu from "@/views/components/checkStu";
export default {
  components: {
    checkStu,
    upload
  },
  data() {
    return {
      total: 0,
      isEdit: false,
      dialogFormVisible: false,
      dialogLook: false,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      searchForm: {
        name: "",
        phone: ""
      },
      addeditform: {
        stuid: '',
        imgurl: "",
        title: "",
        videourl: "",
        videoindex: "",
        isEffect: 2,
        videoid: ''
      },
      rules: {
        stuid: [
          { required: true, message: '必须选择学员', trigger: 'change' },
        ],
        imgurl: [
          { required: true, message: '必须上传视频封面', trigger: 'change' }
        ],
        videourl: [
          { required: true, message: '必须上传视频', trigger: 'change' }
        ],
        videoindex: [
          { required: true, message: '必须设置视频排序', trigger: 'change' },
          { pattern: this.$reg.reg_number, message: '只允许非零正整数', trigger: 'change' }
        ],
        title: [
          { required: true, message: '必须填写视频标题', trigger: 'change' }
        ]
      }
    };
  },
  computed: {
    queryData() {
      return {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    },
    addVform() {
      const { stuid, title, imgurl, isEffect, videourl, videoindex } = this.addeditform
      return { stuid, title, imgurl, isEffect, videourl, videoindex }
    },
    updataData() {
      const { videoid, videoindex, isEffect, title } = this.addeditform
      return {videoid,videoindex,isEffect,title }
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    info(row) {
      this.isEdit = true
      const { imgurl, videourl, videoid, title, videoindex, isEffect } = row
      this.addeditform = {
        imgurl,
        title,
        videourl,
        videoindex,
        isEffect,
        videoid
      }
      this.dialogFormVisible = true
    },
    look(row) {
      this.addeditform.videourl = row.videourl
      this.dialogLook = true
    },
    closeLook() {
      this.$refs.videoPlayer.pause();//暂停
    },
    async getData() {
      const res = await getStuVideo.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.stuVideos;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    checkedItem(val) {
      this.addeditform.stuid = val
    },
    save() {
      console.log(this.addVform, 111);
      this.$refs.addForm.validate((valid) => {
        if (!valid) return false
        if (this.addeditform.videoid) {
          this.updateSVideo()
        } else {
          this.addSVideo()
        }
      })
    },
    async addSVideo() {
      const res = await addStuVideo.post(this.addVform)
      if (res.resultCode === "000000") {
        this.dialogFormVisible = false
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    async updateSVideo() {
      const res = await updateStuVideo.post(this.updataData)
      if (res.resultCode === "000000") {
        this.dialogFormVisible = false
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    addVideo() {
      this.addeditform = {
        stuid: '',
        imgurl: '',
        title: '',
        videourl: '',
        videoindex: '',
        isEffect: 2,
        videoid: ''
      }
      this.isEdit = false
      this.dialogFormVisible = true
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delStuVideo.post({ videoid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="scss" scoped>
.form-item {
  display: flex;
  justify-content: flex-start;
  padding: 20px;
  align-items: center;
}
</style>
